/**
 * 角色配置文件
 * 定义门窗ERP系统中的各种角色及其权限
 */

export interface Role {
  id: string;
  name: string;
  code: string;
  description: string;
  permissions: string[];
  color: string;
  icon: string;
}

export interface RoleModule {
  id: string;
  name: string;
  icon: string;
  path: string;
  roles: string[];
  children?: RoleModule[];
}

// 角色定义
export const ROLES: Role[] = [
  {
    id: 'sales_employee',
    name: '销售员工',
    code: 'SALES_EMPLOYEE',
    description: '负责客户开发、订单管理等销售相关工作',
    permissions: ['crm:customer:view', 'oms:order:view', 'oms:order:create'],
    color: '#1890ff',
    icon: 'cuIcon-people'
  },
  {
    id: 'sales_manager',
    name: '销售管理员',
    code: 'SALES_MANAGER',
    description: '管理销售团队，审核订单，查看销售报表',
    permissions: [
      'crm:customer:all', 'oms:order:all', 'oms:design:all',
      'report:sales:view', 'team:sales:manage'
    ],
    color: '#52c41a',
    icon: 'cuIcon-crown'
  },
  {
    id: 'warehouse_employee',
    name: '仓库员工',
    code: 'WAREHOUSE_EMPLOYEE',
    description: '负责入库、出库、盘点等仓库作业',
    permissions: [
      'wms:inbound:view', 'wms:outbound:view', 'wms:inventory:view',
      'wms:material:view', 'wms:stocktaking:operate'
    ],
    color: '#fa8c16',
    icon: 'cuIcon-goods'
  },
  {
    id: 'warehouse_manager',
    name: '仓库管理员',
    code: 'WAREHOUSE_MANAGER',
    description: '管理仓库运营，审核出入库，管理供应商',
    permissions: [
      'wms:all', 'supplier:manage', 'purchase:manage',
      'report:warehouse:view', 'team:warehouse:manage'
    ],
    color: '#722ed1',
    icon: 'cuIcon-settings'
  },
  {
    id: 'production_employee',
    name: '生产员工',
    code: 'PRODUCTION_EMPLOYEE',
    description: '负责生产任务执行、质量检查等生产作业',
    permissions: [
      'production:task:view', 'production:quality:check',
      'production:progress:update', 'material:requisition:create'
    ],
    color: '#eb2f96',
    icon: 'cuIcon-repair'
  },
  {
    id: 'production_manager',
    name: '生产管理员',
    code: 'PRODUCTION_MANAGER',
    description: '管理生产计划，调度资源，监控生产进度',
    permissions: [
      'production:all', 'production:plan:manage', 'production:schedule:manage',
      'report:production:view', 'team:production:manage'
    ],
    color: '#f5222d',
    icon: 'cuIcon-command'
  }
];

// 功能模块定义
export const ROLE_MODULES: RoleModule[] = [
  {
    id: 'crm',
    name: 'CRM客户管理',
    icon: 'cuIcon-people',
    path: '/pages/crm/index',
    roles: ['SALES_EMPLOYEE', 'SALES_MANAGER'],
    children: [
      {
        id: 'crm_customer',
        name: '客户管理',
        icon: 'cuIcon-group',
        path: '/pages/crm/customer/list',
        roles: ['SALES_EMPLOYEE', 'SALES_MANAGER']
      },
      {
        id: 'crm_contact',
        name: '联系人管理',
        icon: 'cuIcon-phone',
        path: '/pages/crm/contact/list',
        roles: ['SALES_EMPLOYEE', 'SALES_MANAGER']
      },
      {
        id: 'crm_project',
        name: '项目管理',
        icon: 'cuIcon-copy',
        path: '/pages/crm/project/list',
        roles: ['SALES_EMPLOYEE', 'SALES_MANAGER']
      }
    ]
  },
  {
    id: 'oms',
    name: 'OMS订单管理',
    icon: 'cuIcon-list',
    path: '/pages/oms/index',
    roles: ['SALES_EMPLOYEE', 'SALES_MANAGER'],
    children: [
      {
        id: 'oms_order',
        name: '订单管理',
        icon: 'cuIcon-form',
        path: '/pages/oms/order/list',
        roles: ['SALES_EMPLOYEE', 'SALES_MANAGER']
      },
      {
        id: 'oms_design',
        name: '产品设计',
        icon: 'cuIcon-pic',
        path: '/pages/oms/design/list',
        roles: ['SALES_EMPLOYEE', 'SALES_MANAGER']
      }
    ]
  },
  {
    id: 'wms',
    name: 'WMS仓库管理',
    icon: 'cuIcon-goods',
    path: '/pages/wms/index',
    roles: ['WAREHOUSE_EMPLOYEE', 'WAREHOUSE_MANAGER'],
    children: [
      {
        id: 'wms_inventory',
        name: '库存管理',
        icon: 'cuIcon-box',
        path: '/pages/wms/inventory/list',
        roles: ['WAREHOUSE_EMPLOYEE', 'WAREHOUSE_MANAGER']
      },
      {
        id: 'wms_inbound',
        name: '入库管理',
        icon: 'cuIcon-add',
        path: '/pages/wms/inbound/list',
        roles: ['WAREHOUSE_EMPLOYEE', 'WAREHOUSE_MANAGER']
      },
      {
        id: 'wms_outbound',
        name: '出库管理',
        icon: 'cuIcon-move',
        path: '/pages/wms/outbound/list',
        roles: ['WAREHOUSE_EMPLOYEE', 'WAREHOUSE_MANAGER']
      },
      {
        id: 'wms_material',
        name: '物料管理',
        icon: 'cuIcon-album',
        path: '/pages/wms/material/list',
        roles: ['WAREHOUSE_EMPLOYEE', 'WAREHOUSE_MANAGER']
      }
    ]
  },
  {
    id: 'production',
    name: '生产管理',
    icon: 'cuIcon-repair',
    path: '/pages/production/index',
    roles: ['PRODUCTION_EMPLOYEE', 'PRODUCTION_MANAGER'],
    children: [
      {
        id: 'production_task',
        name: '生产任务',
        icon: 'cuIcon-calendar',
        path: '/pages/production/task/list',
        roles: ['PRODUCTION_EMPLOYEE', 'PRODUCTION_MANAGER']
      },
      {
        id: 'production_plan',
        name: '生产计划',
        icon: 'cuIcon-time',
        path: '/pages/production/plan/list',
        roles: ['PRODUCTION_MANAGER']
      },
      {
        id: 'production_quality',
        name: '质量检查',
        icon: 'cuIcon-check',
        path: '/pages/production/quality/list',
        roles: ['PRODUCTION_EMPLOYEE', 'PRODUCTION_MANAGER']
      }
    ]
  }
];

/**
 * 根据角色代码获取角色信息
 */
export function getRoleByCode(code: string): Role | undefined {
  return ROLES.find(role => role.code === code);
}

/**
 * 根据角色代码获取可访问的模块
 */
export function getModulesByRole(roleCode: string): RoleModule[] {
  return ROLE_MODULES.filter(module => 
    module.roles.includes(roleCode)
  );
}

/**
 * 检查角色是否有指定权限
 */
export function hasPermission(roleCode: string, permission: string): boolean {
  const role = getRoleByCode(roleCode);
  return role ? role.permissions.includes(permission) : false;
}

/**
 * 获取角色的主色调
 */
export function getRoleColor(roleCode: string): string {
  const role = getRoleByCode(roleCode);
  return role ? role.color : '#1890ff';
}
